const Router = require('koa-router')
const router = new Router({ prefix: '/role' })

const validator = require('../../middleware/validator')
const Joi = require('joi')

const RoleController = require('../../controller/role')
const Status = require('../../constant/status')

router.get(
  '/',
  validator({
    current: Joi.number(),
    size: Joi.number(),
    code: Joi.string().allow(''),
    name: Joi.string().allow(''),
    status: Joi.string().valid(...Object.values(Status))
  }),
  RoleController.findAllRole
)

router.get('/:id', RoleController.findRoleById)

router.post(
  '/',
  validator({
    code: Joi.string().required(),
    name: Joi.string().required(),
    description: Joi.string(),
    pid: Joi.string().required(),
    status: Joi.string()
      .valid(...Object.values(Status))
      .default('ENABLED')
  }),
  RoleController.createRole
)

router.put(
  '/:id',
  validator({
    id: Joi.string().required(),
    code: Joi.string(),
    name: Joi.string(),
    description: Joi.string(),
    pid: Joi.string(),
    status: Joi.string().valid(...Object.values(Status))
  }),
  RoleController.updateRole
)

router.delete('/:id', RoleController.deleteRole)

module.exports = router
